package com.shankungfu.adapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.shankungfu.MainApplication;
import com.shankungfu.R;
import com.shankungfu.bean.ReplyBean;
import com.shankungfu.main.CommentActivity;
import com.shankungfu.main.ImagePagerActivity;
import com.shankungfu.main.LogInActivity;
import com.shankungfu.main.PublishCommentActivity;
import com.shankungfu.main.SecondCommentActivity;
import com.shankungfu.util.ToastUtil;
import com.shankungfu.view.HorizontalListView;

public class CommentAdapter extends BaseAdapter {

	private Context context;
	private List<ReplyBean> replyBeanList = new ArrayList<ReplyBean>();
	private DisplayImageOptions options;
	private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
	private ImageLoader imageLoader;

	public CommentAdapter(Context context, DisplayImageOptions options) {
		this.context = context;
		this.options = options;
		imageLoader = ImageLoader.getInstance();
	}
	
	public void setData(List<ReplyBean> replyBeanList) {
		if(replyBeanList == null){
			return;
		}
		this.replyBeanList = replyBeanList;
//		notifyDataSetChanged();
	}
	public void addData(List<ReplyBean> replyBeanList) {
		if(replyBeanList == null){
			return;
		}
		this.replyBeanList.addAll(replyBeanList);
//		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return replyBeanList != null ? replyBeanList.size() : 0;
	}

	@Override
	public ReplyBean getItem(int position) {
		return replyBeanList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = View.inflate(context, R.layout.message_child_item_layout,
					null);
			holder.author = (TextView) convertView.findViewById(R.id.message_child_author);
			holder.date = (TextView) convertView.findViewById(R.id.message_child_date);
			holder.content = (TextView) convertView.findViewById(R.id.message_child_content);
			holder.user_icon = (ImageView) convertView.findViewById(R.id.message_child_user_pic);
			holder.comment_count = (TextView) convertView.findViewById(R.id.comment_count);
			holder.pic_listview = (HorizontalListView) convertView
					.findViewById(R.id.pic_listview);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final ReplyBean replyBean = replyBeanList.get(position);
		
		holder.author.setText(replyBean.getReply_user_name());
		holder.date.setText(replyBean.getReply_time());
		holder.content.setText(Html.fromHtml(replyBean.getReply_content()));
		holder.comment_count.setText(replyBean.getComment_count()+"条回复");
		
		imageLoader.displayImage(replyBean.getReply_avatar_img(), holder.user_icon, options, animateFirstListener);
		
		ArrayList<String> img_list = replyBean.getImg_list();
		if (img_list != null && img_list.size() > 0) {
			holder.pic_listview.setVisibility(View.VISIBLE);
			setHorizontalListViewOnItemClick(holder, img_list);
		} else {
			holder.pic_listview.setVisibility(View.GONE);
		}
		
		

		return convertView;
	}
	
	private void startImagePagerActivity(int position, String [] imageUrls) {
		Intent intent = new Intent(context, ImagePagerActivity.class);
		intent.putExtra(com.shankungfu.GlobleConstant.Extra.IMAGES, imageUrls);
		intent.putExtra(com.shankungfu.GlobleConstant.Extra.IMAGE_POSITION,
				position);
		context.startActivity(intent);
	}

	private void setHorizontalListViewOnItemClick(ViewHolder holder, final ArrayList<String> img_list) {
		PickItemDetailAdapter adapter = new PickItemDetailAdapter(context, MainApplication.getOptions());
		adapter.setData(img_list);
		holder.pic_listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				startImagePagerActivity(position, img_list.toArray(new String[]{}));
			}
		});
		holder.pic_listview.setAdapter(adapter);
	}

	private class ViewHolder {
		private TextView author;
		private TextView date;
		private TextView content;
		private ImageView user_icon;
		private TextView comment_count;
		private HorizontalListView pic_listview;
	}
	
	private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {

		static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());

		@Override
		public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
			if (loadedImage != null) {
				ImageView imageView = (ImageView) view;
				boolean firstDisplay = !displayedImages.contains(imageUri);
				if (firstDisplay) {
					FadeInBitmapDisplayer.animate(imageView, 500);
					displayedImages.add(imageUri);
				}
			}
		}
	}

}
